package com.jevons.muffin.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModel;

import com.jevons.muffin.R;
import com.jevons.muffin.crash.CrashConfig;
import com.jevons.muffin.crash.CustomActivityOnCrash;
import com.jevons.muffin.databinding.ActivityDefaultErrorBinding;
import com.jevons.muffin.utilcode.LogUtils;

/**
 * Copyright 2020 JxGIS
 *
 * @file DefaultErrorActivity
 * @auth linaisheng
 * Created on 2023/12/8.
 * Description：
 */
public class DefaultErrorActivity extends BaseActivity {

    private ActivityDefaultErrorBinding dataBinding;
    private CrashConfig crashConfig;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataBinding = DataBindingUtil.setContentView(
                this, R.layout.activity_default_error);
        dataBinding.setLifecycleOwner(this);

        crashConfig = CustomActivityOnCrash.getConfigFromIntent(getIntent());

        dataBinding.btnFinish.setOnClickListener(view -> {
            CustomActivityOnCrash.closeApplication(DefaultErrorActivity.this, crashConfig);
        });
        if (crashConfig.isShowRestartButton()) {
            dataBinding.btnRestart.setOnClickListener(view -> {
                CustomActivityOnCrash.restartApplication(DefaultErrorActivity.this, crashConfig);
            });
        } else {
            dataBinding.btnRestart.setVisibility(View.GONE);
        }

        if (crashConfig.isShowErrorDetails()) {
            dataBinding.btnErrorDetail.setOnClickListener(view -> {
                if (dataBinding.errorInfoBox.getVisibility() != View.VISIBLE) {
                    dataBinding.errorInfoBox.setVisibility(View.VISIBLE);
                    dataBinding.errorInfoMsg.setText(
                            CustomActivityOnCrash.getAllErrorDetailsFromIntent(
                                    DefaultErrorActivity.this, getIntent()));
                }
            });
        } else {
            dataBinding.btnErrorDetail.setVisibility(View.GONE);
        }

        dataBinding.btnCopyError.setOnClickListener(view -> {
            copyErrorToClipboard();
            Toast.makeText(DefaultErrorActivity.this, "复制成功", Toast.LENGTH_SHORT).show();
        });

        LogUtils.printError("\n" + CustomActivityOnCrash.getStackTraceFromIntent(getIntent()));
    }

    @Override
    public void onBackPressed() {
        CustomActivityOnCrash.closeApplication(DefaultErrorActivity.this, crashConfig);
    }

    @Override
    protected ViewModel initViewModel() {
        return null;
    }

    private void copyErrorToClipboard() {
        String errorInformation = CustomActivityOnCrash.getAllErrorDetailsFromIntent(DefaultErrorActivity.this, getIntent());

        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("错误信息", errorInformation);
        clipboard.setPrimaryClip(clip);
    }

}